//
//  LBZaiLineVC.m
//  V-Learn
//
//  Created by Apple on 2018/12/31.
//  Copyright © 2018 yxk. All rights reserved.
//

#import "LBZaiLineVC.h"
#import "LBZaiLineCell.h"
#import "BRStringPickerView.h"
#import "LBPayVC.h"
#import "WKOnlinePayVC.h"
#import "LBVipEvaluationResultVC.h"
#import "WKOnlinePingguResultModel.h"
#import "VKPersonInfoModel.h"
@interface LBZaiLineVC ()<UITableViewDelegate,UITableViewDataSource,UITextFieldDelegate>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) UILabel *ageLab;
@property (nonatomic, strong) UILabel *monthLab;
@property (nonatomic, strong) UILabel *timeLab;
@property (nonatomic, strong) NSArray *titleArr;
@property (nonatomic, strong) NSString *ageString;
@property (nonatomic, strong) NSString *monthString;
@property (nonatomic, strong) NSString *timeLongString;
@property (nonatomic, strong) NSArray *dataArray;
@property (nonatomic, strong) VKPersonInfoModel *personInfoModel;
@end
@implementation LBZaiLineVC
- (NSArray *)titleArr {
    if (!_titleArr) {
        _titleArr = @[@"孩子年龄",@"孩子月份",@"每天学习时长"];
    }
    return _titleArr;
}
- (UILabel *)ageLab{
    if (!_ageLab) {
        _ageLab = [UILabel labelWithText:@"" andTextColor:rgb(51,51,51) andName:nil andFontSize:14*ScaleX];
    }
    return _ageLab;
}
- (UILabel *)monthLab{
    if (!_monthLab) {
        _monthLab = [UILabel labelWithText:@"" andTextColor:rgb(51,51,51) andName:nil andFontSize:14*ScaleX];
    }
    return _monthLab;
}
- (UILabel *)timeLab{
    if (!_timeLab) {
        _timeLab = [UILabel labelWithText:@"" andTextColor:rgb(51,51,51) andName:nil andFontSize:14*ScaleX];
    }
    return _timeLab;
}
- (void)viewDidLoad{
    [super viewDidLoad];
    self.navigationItem.title = @"在线规划";

    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, LBNAVBAR_HEIGHT, ScreenW, ScreenH-LBNAVBAR_HEIGHT) style:UITableViewStylePlain];
    self.tableView.backgroundColor = rgb(242,242,242);
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    if (@available(iOS 11.0, *)) {
        self.tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    }else {
        self.automaticallyAdjustsScrollViewInsets = NO;
    }
    UIView *footerView = [UIView new];
    footerView.backgroundColor = rgb(242,242,242);
    
    self.tableView.tableFooterView = footerView;
    footerView.frame = CGRectMake(0, 0, ScreenW, 300);
    [self.view addSubview:self.tableView];
    
    UIButton *productButton = [[UIButton alloc] init];
    [self.view addSubview:productButton];
    productButton.layer.cornerRadius = 22;
    productButton.layer.masksToBounds = YES;
    [productButton makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(15);
        make.right.mas_equalTo(-15);
        make.height.mas_equalTo(44);
        make.bottom.mas_equalTo(-50);
    }];
    [productButton setTitle:@"生成规划" forState:UIControlStateNormal];
    [productButton setTitleColor:rgb(255,255,255) forState:UIControlStateNormal];
    productButton.titleLabel.font = LBFont(18);
//    productButton.backgroundColor = rgb(233,84,18);
    [productButton setBackgroundImage:LBImage(@"wk_shadow_btn") forState:UIControlStateNormal];
    [productButton addTarget:self action:@selector(clickDingzhiBtn:) forControlEvents:UIControlEventTouchUpInside];
    
    //查看vip接口
    
    [self requestDataIsvip];
}
- (void)requestDataIsvip{
    NSString *token = [UserModel getUserModel].token;
    [WYHXNetWorkService netWorkSerViceRequestWithGetUrl:WKMeUserInfo withPostParams:@{@"token":token == nil ? @"":token}.mutableCopy withBlock:^(NSDictionary *result, NSError *error) {
        NSString *code = result[@"code"];
        
        if ([code integerValue]==200) {
            VKPersonInfoModel *model = [VKPersonInfoModel mj_objectWithKeyValues:result[@"data"]];
            self.personInfoModel = model;
        }else if ([code integerValue]==401){
            
        }
        [self.tableView reloadData];
    }];
}
- (void)clickDingzhiBtn:(UIButton *)sender{
    
    if (self.ageString.length==0) {
        [MBProgressHUD showSuccess:@"请选择年龄" toView:self.view];
        return;
    }
    if (self.monthString.length==0) {
        [MBProgressHUD showSuccess:@"请选择月份" toView:self.view];
        return;
    }
    if (self.timeLongString.length==0) {
        [MBProgressHUD showSuccess:@"请选择时长" toView:self.view];
        return;
    }

    if ([self.personInfoModel.isVip integerValue]==1) {
        
        NSMutableDictionary *data = [NSMutableDictionary dictionary];
        NSString *token = [UserModel getUserModel].token;
        [data setObject:token forKey:@"token"];
        [data setObject:self.ageString forKey:@"ageYear"];
        [data setObject:self.monthString forKey:@"ageMonth"];
        [data setObject:self.timeLongString forKey:@"courseLength"];
        
        [data setObject:@"1" forKey:@"planType"];
        
        [WYHXNetWorkManager post:WKQuerycommitPlanByVip_WKURL params:data completionHandler:^(id responseObj, NSError *error) {
            NSString *code = responseObj[@"code"];
            if ([code integerValue]==200) {
                self.dataArray = [WKOnlinePingguResultModel mj_objectArrayWithKeyValuesArray:responseObj[@"data"]];

                LBVipEvaluationResultVC *vc = [[LBVipEvaluationResultVC alloc] init];
                vc.dataArray = self.dataArray;
                [self.navigationController pushViewController:vc animated:YES];
                
            }
        }];
        
    }else{
        [WYHXNetWorkManager get:WKQueryOnLinePlanPrice_WKURL params:nil completionHandler:^(id responseObj, NSError *error) {
            NSString *code = responseObj[@"code"];
            if ([code integerValue]==200) {
                NSString *price = responseObj[@"data"];
                NSString *titleString = @"在线规划";
                WKOnlinePayVC *payVC = [[WKOnlinePayVC alloc] init];
                payVC.titleString = titleString;
                payVC.price = [NSString stringWithFormat:@"%@",price];
                payVC.planType = @"1";
                payVC.ageMonth = self.monthString;
                payVC.ageYear = self.ageString;
                payVC.tel = @"";
                payVC.courseLength = self.timeLongString;
                [self.navigationController pushViewController:payVC animated:YES];
            }
        }];
    }
//WKQueryOnLinePlanPrice_WKURL

    

}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 3;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *cellID = @"testCell";
    LBZaiLineCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (!cell) {
        cell = [[LBZaiLineCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
    }

    cell.selectionStyle = UITableViewCellSelectionStyleNone;

    
    cell.titleLabel.text = self.titleArr[indexPath.section];
    cell.textField.delegate = self;
    cell.textField.tag = indexPath.section;
    switch (indexPath.section) {
        case 0:
        {
            cell.isNeed = YES;
            cell.isNext = YES;
            cell.textField.placeholder = @"请选择";
//            cell.textField.text = self.infoModel.genderStr;
        }
            break;
        case 1:
        {
            cell.isNeed = YES;
            cell.isNext = YES;
            cell.textField.placeholder = @"请选择";
//            cell.textField.text = self.infoModel.birthdayStr;
        }
            break;
        case 2:
        {
            cell.isNeed = NO;
            cell.isNext = YES;
            cell.textField.placeholder = @"请选择";
//            cell.textField.text = self.infoModel.birthtimeStr;
        }
            break;
            
        default:
            break;
    }

    return cell;

}

#pragma mark - UITextFieldDelegate 返回键
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
//    if (textField.tag == 0 || textField.tag == 4) {
////        [textField resignFirstResponder];
//    }
    return YES;
}

#pragma mark - UITextFieldDelegate
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
    if (textField.tag == 10) {
        [textField addTarget:self action:@selector(handlerTextFieldEndEdit:) forControlEvents:UIControlEventEditingDidEnd];
        return NO; // 当前 textField 可以编辑
    } else {
        [self.view endEditing:YES];
        [self handlerTextFieldSelect:textField];
        return NO; // 当前 textField 不可编辑，可以响应点击事件
    }
}
- (void)handlerTextFieldSelect:(UITextField *)textField {
    __weak typeof(self)weakSelf = self;
    switch (textField.tag) {
        case 0:
        {
            [BRStringPickerView showStringPickerWithTitle:@"孩子年龄" dataSource:@[@"3岁", @"4岁", @"5岁",@"6岁", @"7岁", @"8岁"] defaultSelValue:textField.text resultBlock:^(id selectValue) {
                textField.text = selectValue;

                self.ageString = [selectValue stringByReplacingOccurrencesOfString:@"岁" withString:@""];
                
            }];
        
        }
            break;
        case 1:
        {
            [BRStringPickerView showStringPickerWithTitle:@"孩子月份" dataSource:@[@"1月", @"2月", @"3月",@"4月", @"5月", @"6月",@"7月", @"8月", @"9月",@"10月",@"11月",@"12月"] defaultSelValue:textField.text resultBlock:^(id selectValue) {
                textField.text = selectValue;
                self.monthString = [selectValue stringByReplacingOccurrencesOfString:@"月" withString:@""];
            }];
            
        }
            break;
        case 2:
        {
            [BRStringPickerView showStringPickerWithTitle:@"学习时长" dataSource:@[@"1小时",@"2小时",@"3小时",@"4小时", @"5小时", @"6小时",@"7小时", @"8小时"] defaultSelValue:textField.text resultBlock:^(id selectValue) {
                textField.text = selectValue;
                self.timeLongString = [selectValue stringByReplacingOccurrencesOfString:@"小时" withString:@""];
            }];
        }
            break;
    }
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{

    return 10;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{

    UIView *headerView = [UIView new];
    headerView.backgroundColor = rgb(242,242,242);
    return headerView;
}
@end
